package com.example.lenovo.weiyi;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.com.ruanmeng.demon.MyOrderM;
import com.com.ruanmeng.utils.CommonUtil;
import com.com.ruanmeng.utils.Params;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import adapter.BaseLoadMoreHeaderAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

public class MyOrderActivity extends BaseActivity {

    @BindView(R.id.rb_order_all)
    RadioButton rbOrderAll;
    @BindView(R.id.rb_order_nopay)
    RadioButton rbOrderNopay;
    @BindView(R.id.rb_order_payed)
    RadioButton rbOrderPayed;
    @BindView(R.id.rlist_myorder)
    RecyclerView rlistMyorder;
    @BindView(R.id.refresh_myorder)
    SwipeRefreshLayout mRefresh;
    private LinearLayout li_myservice_null;
    ArrayList<MyOrderM.RowsBean> Temp_list = new ArrayList<>();
    MyServiceListAdapter mAdapter;
    //NOPAY(0, "未支付"), PAY(1, "已支付");   全部  空
    String payStatus = "";
    int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order);
        ButterKnife.bind(this);
        ChangLayTitle("我的订单");
        LayBack();
        init();
        getData(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Params.PaySucess == 1) {
            Params.PaySucess = 0;
            getData(false);
        }
    }

    private void init() {
        //空
        li_myservice_null = (LinearLayout) findViewById(R.id.li_myorder_null);
        mRefresh.setColorSchemeResources(R.color.MainColor);
        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData(true);
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rlistMyorder.setLayoutManager(linearLayoutManager);
        mAdapter = new MyServiceListAdapter(MyOrderActivity.this, rlistMyorder, Temp_list, R.layout.item_myorder);
        rlistMyorder.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseLoadMoreHeaderAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent in = new Intent(MyOrderActivity.this, OrderDetailsActivity.class);
                in.putExtra("orderId", Temp_list.get(position + 1).getOrderId());
                if (Temp_list.get(position + 1).getPayStatus() == 0) {
                    in.putExtra("FromDD", "1");
                }
                startActivity(in);

            }
        });
        mAdapter.setOnLoadMoreListener(new BaseLoadMoreHeaderAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getData(false);
            }
        });
    }

    /**
     * 获取我的订单
     */
    public void getData(Boolean b) {
        //D29B7A2E901C446494E4B50FBEBFA788
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.MyOrder, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(MyOrderActivity.this, "uid"));
        mRequest.add("payStatus", payStatus);
        mRequest.add("page", page);
        // 添加到请求队列
        CallServer.getRequestInstance().add(MyOrderActivity.this, 0, mRequest,
                new CustomHttpListener(MyOrderActivity.this, true, MyOrderM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        MyOrderM model = ((MyOrderM) data);
                        if (page == 1) {
                            Temp_list.clear();
                        }
                        Temp_list.addAll(model.getRows());
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        if (!("100").equals(obj.getString("msgcode"))) {
                            CommonUtil.showToask(MyOrderActivity.this, obj.getString("msg"));
                        }
                        if (Temp_list.size() == 0) {
                            li_myservice_null.setVisibility(View.VISIBLE);
                            rlistMyorder.setVisibility(View.GONE);
                        } else {
                            li_myservice_null.setVisibility(View.GONE);
                            rlistMyorder.setVisibility(View.VISIBLE);
                        }
                        mRefresh.setRefreshing(false);
                        mAdapter.setLoading(false);
                    }
                }, true, b);
    }


    public class MyServiceListAdapter extends BaseLoadMoreHeaderAdapter<MyOrderM.RowsBean> {


        public MyServiceListAdapter(Context mContext, RecyclerView recyclerView, List<MyOrderM.RowsBean> mDatas, int mLayoutId) {
            super(mContext, recyclerView, mDatas, mLayoutId);
        }

        @Override
        public void convert(Context mContext, RecyclerView.ViewHolder holder, final MyOrderM.RowsBean s) {
            TextView name = (TextView) holder.itemView.findViewById(R.id.tv_myorder_name);
            TextView time = (TextView) holder.itemView.findViewById(R.id.tv_myorder_time);
            final TextView statues = (TextView) holder.itemView.findViewById(R.id.tv_myorder_statues);
            View li_ban = (View) holder.itemView.findViewById(R.id.view_myorder_ban);
            View view_myorder_man = (View) holder.itemView.findViewById(R.id.view_myorder_man);
            name.setText(s.getSubject());
            time.setText("（" + s.getCreateDate() + "）");
            ////NOPAY(0, "未支付"), PAY(1, "已支付");
            if (s.getPayStatus() == 0) {
                statues.setText("未支付");
                statues.setTextColor(getResources().getColor(R.color.MainColor));
            } else {
                statues.setText("已支付");
                statues.setTextColor(getResources().getColor(R.color.ThirdGrayColor));
            }
            if (holder.getLayoutPosition() == Temp_list.size() - 1) {
                view_myorder_man.setVisibility(View.VISIBLE);
                li_ban.setVisibility(View.GONE);
            } else {
                view_myorder_man.setVisibility(View.GONE);
                li_ban.setVisibility(View.VISIBLE);
            }
            statues.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if ("未支付".equals(statues.getText().toString())) {
                        Intent in = new Intent(MyOrderActivity.this, OrderDetailsActivity.class);
                        in.putExtra("FromDD", "1");
                        in.putExtra("orderId", s.getOrderId());
                        in.putExtra("name", s.getSubject());
                        in.putExtra("price", s.getAmount() + "");
                        in.putExtra("num", s.getCount() + "");
                        startActivity(in);
                    }

                }
            });
        }
    }

    public void doClick(View v) {
        switch (v.getId()) {
            ////NOPAY(0, "未支付"), PAY(1, "已支付");   全部  空
            case R.id.rb_order_all://全部
                payStatus = "";
                break;
            case R.id.rb_order_nopay://未支付
                payStatus = "0";
                break;
            case R.id.rb_order_payed://已支付
                payStatus = "1";
                break;
        }
        page = 1;
        getData(true);
    }
}
